﻿using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.SharePoint.Explorer;
using System.Collections.Generic;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System.Linq;
using System.Linq.Expressions;
using System;


namespace ServerExplorer.SharePointConnections.SharepointSiteInfo
{
    // Enables Visual Studio to discover and load this extension.
    [Export(typeof(IExplorerNodeTypeExtension))]

    // Indicates that this class extends SharePoint site nodes in Server Explorer.
    [ExplorerNodeType(ExplorerNodeTypes.SiteNode)]

    public class SharepointSiteInfoExtensios : IExplorerNodeTypeExtension
    {
        public void Initialize(IExplorerNodeType nodeType)
        {
            nodeType.NodeChildrenRequested += NodeChildrenRequested;
        }

        //void NodeMenuItemsRequested(object sender, ExplorerNodeMenuItemsRequestedEventArgs e)
        //{
        //    string typeNode = e.Node.Text;
        //    if (typeNode == Properties.Resources.SiteInfoNodeName)
        //    {
        //        if (e.MenuItems.Count(c => c.Text == "Show Window") == 0)
        //        {
        //            IMenuItem menuItem = e.MenuItems.Add("Show Window");
        //            menuItem.Click += menuItem_Click;
        //        }
        //    }
        //}

        // Creates the new Info Site node with the specified icon.
        private void NodeChildrenRequested(object sender, ExplorerNodeEventArgs e)
        {
            IExplorerNode main = e.Node.ChildNodes.AddFolder(Properties.Resources.SiteInfoNodeName, null, CreateSiteInfoNode);
            //main.NodeType.NodeMenuItemsRequested += NodeMenuItemsRequested;
        }

        // Creates all of the individual INFO nodes under the site node.
        private void CreateSiteInfoNode(IExplorerNode parentNode)
        {
            // Call the custom SharePoint command to get items from site
            Entities.SiteInfo[] infoSite = parentNode.Context.SharePointConnection.ExecuteCommand<Entities.SiteInfo[]>(SharepointSiteInfoCommandsIDs.GetSharepointSiteInfo);

            foreach (Entities.SiteInfo info in infoSite)
            {
                // Create a new annotation object to store the current SiteInfo item with the new node. 
                var annotations = new Dictionary<object, object>() 
                    { 
                        { "Info", info } 
                    };

                // Create the new node for the current SiteInfo item.
                IExplorerNode iCulture = parentNode.ChildNodes.Add(
                            Microsoft.VisualStudio.SharePoint.Explorer.ExplorerNodeTypes.GenericFolderNode,
                            info.CultureName,
                            null);

                IExplorerNode iListFolder = iCulture.ChildNodes.Add(SharepointInfoSiteNode.SharepointInfoSiteNodeTypeId, "Lists", annotations);

                foreach (Entities.SiteInfoList infoList in info.SiteLists)
                {
                    annotations = new Dictionary<object, object>() 
                    { 
                        { "Info", infoList } 
                    };
                    IExplorerNode iList = iListFolder.ChildNodes.Add(SharepointInfoSiteNode.SharepointInfoSiteNodeTypeId, infoList.IntenalName, annotations);
                    IExplorerNode iEventChild = iList.ChildNodes.Add(Microsoft.VisualStudio.SharePoint.Explorer.ExplorerNodeTypes.GenericFolderNode,
                        "EventReceivers", null);

                    foreach (Entities.SiteInfoEventReceiver evField in infoList.SiteEventReceivers)
                    {
                        IExplorerNode ievChild = iEventChild.ChildNodes.Add(Microsoft.VisualStudio.SharePoint.Explorer.ExplorerNodeTypes.GenericFolderNode,
                                                                           evField.EventClass, null);
                    }

                    IExplorerNode iListChild = iList.ChildNodes.Add(Microsoft.VisualStudio.SharePoint.Explorer.ExplorerNodeTypes.GenericFolderNode,
                                               "Fields", null);
                    string[] arg = new string[] { infoList.Guid, info.CultureName };
                    Entities.SiteInfoField[] infoField = parentNode.Context.SharePointConnection.ExecuteCommand<string[], Entities.SiteInfoField[]>(SharepointSiteInfoCommandsIDs.GetSharepointSiteFieldInfo, arg);
                    foreach (Entities.SiteInfoField fInfo in infoField)
                    {
                        annotations = new Dictionary<object, object>() 
                       { 
                         { "Info", fInfo } 
                       };
                        IExplorerNode iliChild = iListChild.ChildNodes.Add(SharepointInfoSiteNode.SharepointInfoSiteNodeTypeId, fInfo.InternalName, annotations);
                    }
                }
            }
        }
    }
}
